diff --git a/index.js b/index.js deleted file mode 100644 index efcf7f1..0000000 --- a/index.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - MIT License http://www.opensource.org/licenses/mit-license.php - Author Tobias Koppers @sokra -*/ - -const { getOptions } = require('loader-utils'); -const validateOptions = require('schema-utils'); - -const schema = require('./options'); - -module.exports = function rawLoader(source) { - const options = getOptions(this) || {}; - - validateOptions(schema, options, 'Raw Loader'); - - const json = JSON.stringify(source) - .replace(/\u2028/g, '\\u2028') - .replace(/\u2029/g, '\\u2029'); - return `module.exports = ${json}`; -}; diff --git a/package.json b/package.json index 7f4555d..d85d335 100644 --- a/package.json +++ b/package.json @@ -7,20 +7,24 @@ "author": "Tobias Koppers @sokra", "homepage": "https://github.com/webpack-contrib/raw-loader", "bugs": "https://github.com/webpack-contrib/raw-loader/issues", - "main": "index.js", + "main": "dist/cjs.js", "engines": { "node": ">= 6.9.0" }, "scripts": { "start": "npm run build -- -w", + "build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files", + "clean": "del-cli dist", "commitlint": "commitlint", "commitmsg": "commitlint -e $GIT_PARAMS", - "lint": "eslint --cache index.js test", + "lint": "eslint --cache src test", + "prebuild": "npm run clean", + "prepublish": "npm run build", "release": "standard-version", "security": "npm audit", "test": "jest", "test:watch": "jest --watch", - "test:coverage": "jest --collectCoverageFrom='index.js' --coverage", + "test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage", "ci:lint": "npm run lint && npm run security", "ci:test": "npm run test -- --runInBand", "ci:coverage": "npm run test:coverage -- --runInBand", @@ -28,8 +32,7 @@ "defaults": "webpack-defaults" }, "files": [ - "index.js", - "options.json" + "dist/" ], "peerDependencies": { "webpack": "^4.3.0" diff --git a/src/cjs.js b/src/cjs.js new file mode 100644 index 0000000..90fe4df --- /dev/null +++ b/src/cjs.js @@ -0,0 +1,3 @@ +const loader = require('./index'); + +module.exports = loader.default; diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..4946c90 --- /dev/null +++ b/src/index.js @@ -0,0 +1,16 @@ +import { getOptions } from 'loader-utils'; +import validateOptions from 'schema-utils'; + +import schema from './options.json'; + +export default function rawLoader(source) { + const options = getOptions(this) || {}; + + validateOptions(schema, options, 'Raw Loader'); + + const json = JSON.stringify(source) + .replace(/\u2028/g, '\\u2028') + .replace(/\u2029/g, '\\u2029'); + + return `module.exports = ${json}`; +} diff --git a/options.json b/src/options.json similarity index 100% rename from options.json rename to src/options.json diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 10745e2..01b8252 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -4,6 +4,8 @@ exports[`loader should works: errors 1`] = `Array []`; exports[`loader should works: file 1`] = `"module.exports = \\"Где розы — там и тернии —\\\\nТаков закон судьбы.\\\\n\\\\nНиколай Алексеевич Некрасов\\\\n\\\\nWhere the roses are - there are thorns -\\\\nThat is the law of fate.\\\\n\\\\nNikolay Alekseevich Nekrasov\\\\n\\""`; +exports[`loader should works: inline 1`] = `"module.exports = \\"Где розы — там и тернии —\\\\nТаков закон судьбы.\\\\n\\\\nНиколай Алексеевич Некрасов\\\\n\\\\nWhere the roses are - there are thorns -\\\\nThat is the law of fate.\\\\n\\\\nNikolay Alekseevich Nekrasov\\\\n\\""`; + exports[`loader should works: separator 1`] = `"module.exports = \\"Word\\\\u2028Word\\\\u2029Word\\\\n\\""`; exports[`loader should works: warnings 1`] = `Array []`; diff --git a/test/__snapshots__/errors.test.js.snap b/test/__snapshots__/validation.test.js.snap similarity index 100% rename from test/__snapshots__/errors.test.js.snap rename to test/__snapshots__/validation.test.js.snap diff --git a/test/cjs.test.js b/test/cjs.test.js new file mode 100644 index 0000000..8aba6ba --- /dev/null +++ b/test/cjs.test.js @@ -0,0 +1,8 @@ +import src from '../src'; +import cjs from '../src/cjs'; + +describe('cjs', () => { + it('should exported', () => { + expect(cjs).toEqual(src); + }); +}); diff --git a/test/fixtures/basic.js b/test/fixtures/basic.js index 614329d..054417c 100644 --- a/test/fixtures/basic.js +++ b/test/fixtures/basic.js @@ -1,4 +1,7 @@ +// eslint-disable-next-line import/no-webpack-loader-syntax, import/no-unresolved +import inline from '!!../../src/index.js!./file.txt'; + import txt from './file.txt'; import separator from './separator.txt'; -export { txt, separator }; +export { txt, separator, inline }; diff --git a/test/helpers.js b/test/helpers.js index b3ba81e..12873e4 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -13,7 +13,7 @@ const moduleConfig = (config) => { test: (config.loader && config.loader.test) || /\.txt/, use: [ { - loader: path.resolve(__dirname, '../index.js'), + loader: path.resolve(__dirname, '../src/index.js'), options: (config.loader && config.loader.options) || {}, }, ], diff --git a/test/loader.test.js b/test/loader.test.js index 41d41bb..0086268 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -4,10 +4,12 @@ describe('loader', () => { it('should works', async () => { const stats = await webpack('basic.js'); const { modules } = stats.toJson(); - const [, file, separator] = modules; + const [inline, , file, separator] = modules; + expect(inline.source).toMatchSnapshot('inline'); expect(file.source).toMatchSnapshot('file'); expect(separator.source).toMatchSnapshot('separator'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); expect(stats.compilation.errors).toMatchSnapshot('errors'); }); diff --git a/test/errors.test.js b/test/validation.test.js similarity index 91% rename from test/errors.test.js rename to test/validation.test.js index a35934e..6c2ad2e 100644 --- a/test/errors.test.js +++ b/test/validation.test.js @@ -1,4 +1,4 @@ -import loader from '../index'; +import loader from '../src'; it('validation', () => { const validate = (options) =>