Skip to content

feat: allowing extension list to feature extensions with two dots (e.g. module.less, module.css) #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { dirname, extname, resolve } = require('path')
const { dirname, resolve } = require('path')
const transform = require('./transform')

const defaultOptions = {
Expand All @@ -17,10 +17,11 @@ const getVariableName = p => {
}

const applyTransform = (p, t, state, value, calleeName) => {
const ext = extname(value)
const options = Object.assign({}, defaultOptions, state.opts)

if (options.extensions && options.extensions.indexOf(ext.slice(1)) >= 0) {
const valueMatches = options.extensions.some(ext => value.endsWith(ext))

if (options.extensions && valueMatches) {
try {
const rootPath = state.file.opts.sourceRoot || process.cwd()
const scriptDirectory = dirname(resolve(state.file.opts.filename))
Expand Down
4 changes: 1 addition & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ function transform (rootPath, filePath, opts) {

const parsed = path.parse(filePath)

if (parsed.ext) {
ext = parsed.ext.substr(1)
}
ext = parsed.base.slice(parsed.base.indexOf('.') + 1)

let basePath

Expand Down
4 changes: 3 additions & 1 deletion test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`index doesnt inline file when the lenght equals the limit 1`] = `"const test = \\"/public/test/assets/file.txt\\";"`;
exports[`index doesnt inline file when the length equals the limit 1`] = `"const test = \\"/public/test/assets/file.txt\\";"`;

exports[`index handles base26 digest 1`] = `"const test = \\"/public/ccjesbcwzsppzgsqtbnioeblcexw.png\\";"`;

Expand Down Expand Up @@ -34,6 +34,8 @@ exports[`index handles hash as query param 1`] = `"const test = \\"/public/test/

exports[`index handles hex digest 1`] = `"const test = \\"/public/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;

exports[`index handles multiple extensions 1`] = `"const test = \\"/static/934823cbc67ccf0d67aa2a2eeb798f12.module.less\\";"`;

exports[`index handles name with custom hash length 1`] = `"const test = \\"/public/9c87cbf3.png\\";"`;

exports[`index handles sha1 hash 1`] = `"const test = \\"/public/f824ad7c5f655c81c88d3fe267fe5780055bced5.png\\";"`;
Expand Down
3 changes: 3 additions & 0 deletions test/assets/file.module.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: red;
}
1 change: 1 addition & 0 deletions test/fixtures/import-multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import test from '../assets/file.module.less'
12 changes: 10 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('index', function () {
expect(result).toMatchSnapshot()
})

it('handles multiple extensions', function () {
const result = transformCode(getFixtures('import-multiple.js'), {
publicPath: '/static',
extensions: ['module.less']
}).code
expect(result).toMatchSnapshot()
})

it('handles custom import path with trailing slash', function () {
const result = transformCode(getFixtures('import-image.js'), {
publicPath: '/static/'
Expand Down Expand Up @@ -260,7 +268,7 @@ describe('index', function () {
expect(fs.existsSync(path.resolve(__dirname, './public'))).toEqual(false)
})

it('doesnt inline file when the lenght equals the limit', function () {
it('doesnt inline file when the length equals the limit', function () {
const result = transformCode(getFixtures('import-text.js'), {
extensions: ['txt'],
name: '[path][name].[ext]',
Expand All @@ -269,7 +277,7 @@ describe('index', function () {
expect(result).toMatchSnapshot()
})

it('ouputs the file when the lenght equals the limit', function () {
it('ouputs the file when the length equals the limit', function () {
transformCode(getFixtures('import-text.js'), {
extensions: ['txt'],
name: '[path][name].[ext]',
Expand Down