From 75ca0aabe671f894dcfdb3d04fb3bc516284cb76 Mon Sep 17 00:00:00 2001 From: Robbie Cook Date: Sun, 7 Nov 2021 15:09:50 +1300 Subject: [PATCH] fix: fixing extensions fix: add tests fix: fix fix: fix --- src/index.js | 7 ++++--- src/transform.js | 4 +--- test/__snapshots__/index.test.js.snap | 4 +++- test/assets/file.module.less | 3 +++ test/fixtures/import-multiple.js | 1 + test/index.test.js | 12 ++++++++++-- 6 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 test/assets/file.module.less create mode 100644 test/fixtures/import-multiple.js diff --git a/src/index.js b/src/index.js index f5a6e33..4a3497b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const { dirname, extname, resolve } = require('path') +const { dirname, resolve } = require('path') const transform = require('./transform') const defaultOptions = { @@ -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)) diff --git a/src/transform.js b/src/transform.js index 9d1d120..c6c06d9 100644 --- a/src/transform.js +++ b/src/transform.js @@ -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 diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 4938410..9e3842c 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -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\\";"`; @@ -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\\";"`; diff --git a/test/assets/file.module.less b/test/assets/file.module.less new file mode 100644 index 0000000..aa1634c --- /dev/null +++ b/test/assets/file.module.less @@ -0,0 +1,3 @@ +body { + background-color: red; +} diff --git a/test/fixtures/import-multiple.js b/test/fixtures/import-multiple.js new file mode 100644 index 0000000..b8f67d8 --- /dev/null +++ b/test/fixtures/import-multiple.js @@ -0,0 +1 @@ +import test from '../assets/file.module.less' diff --git a/test/index.test.js b/test/index.test.js index 8f3fc7b..cbfafd7 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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/' @@ -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]', @@ -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]',