forked from shakacode/shakapacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed babel configuration for shakacode#208 (shakacode#240)
* fixed babel configuration for shakacode#208
- Loading branch information
Showing
12 changed files
with
358 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const webpack = require("webpack"); | ||
const MemoryFS = require("memory-fs"); | ||
const thenify = require("thenify"); | ||
const path = require("path"); | ||
|
||
const createTrackLoader = () => { | ||
const filesTracked = {}; | ||
return [ | ||
filesTracked, | ||
(source) => { | ||
filesTracked[source.resource] = true; | ||
return source; | ||
}, | ||
]; | ||
}; | ||
|
||
const node_modules = path.resolve("node_modules"); | ||
const node_modules_included = path.resolve("node_modules/included"); | ||
const app_javascript = path.resolve("app/packs"); | ||
|
||
const createInMemoryFs = () => { | ||
const fs = new MemoryFS(); | ||
|
||
fs.mkdirpSync(node_modules); | ||
fs.mkdirpSync(node_modules_included); | ||
fs.mkdirpSync(app_javascript); | ||
|
||
return fs; | ||
}; | ||
|
||
const createTestCompiler = (config, fs = createInMemoryFs()) => { | ||
Object.values(config.entry).forEach((file) => { | ||
fs.writeFileSync(file, "console.log(1);"); | ||
}); | ||
|
||
const compiler = webpack(config); | ||
compiler.run = thenify(compiler.run); | ||
compiler.inputFileSystem = fs; | ||
compiler.outputFileSystem = fs; | ||
return compiler; | ||
}; | ||
|
||
module.exports = { | ||
createTrackLoader, | ||
node_modules, | ||
node_modules_included, | ||
app_javascript, | ||
createInMemoryFs, | ||
createTestCompiler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const path = require("path"); | ||
const { | ||
app_javascript, | ||
node_modules, | ||
node_modules_included, | ||
createTestCompiler, | ||
createTrackLoader, | ||
} = require("./__utils__/webpack"); | ||
const babelConfig = require("../babel"); | ||
|
||
jest.mock("../../config", () => { | ||
const original = jest.requireActual("../../config"); | ||
return { | ||
...original, | ||
additional_paths: [...original.additional_paths, "node_modules/included"], | ||
}; | ||
}); | ||
|
||
const createWebpackConfig = (file, use) => { | ||
return { | ||
entry: { file }, | ||
module: { | ||
rules: [ | ||
{ | ||
...babelConfig, | ||
use, | ||
}, | ||
], | ||
}, | ||
output: { | ||
path: "/", | ||
filename: "scripts-bundled.js", | ||
}, | ||
}; | ||
}; | ||
|
||
describe("babel", () => { | ||
test("process source path", async () => { | ||
const normalPath = `${app_javascript}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler( | ||
createWebpackConfig(normalPath, loader) | ||
); | ||
await compiler.run(); | ||
expect(tracked[normalPath]).toBeTruthy(); | ||
}); | ||
|
||
test("exclude node_modules", async () => { | ||
const ignored = `${node_modules}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(ignored, loader)); | ||
await compiler.run(); | ||
expect(tracked[ignored]).toBeUndefined(); | ||
}); | ||
|
||
test("explicitly included node_modules should be transpiled", async () => { | ||
const included = `${node_modules_included}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(included, loader)); | ||
await compiler.run(); | ||
expect(tracked[included]).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const path = require("path"); | ||
const { | ||
app_javascript, | ||
node_modules, | ||
node_modules_included, | ||
createTestCompiler, | ||
createTrackLoader, | ||
} = require("./__utils__/webpack"); | ||
const esbuildConfig = require("../esbuild"); | ||
|
||
jest.mock("../../config", () => { | ||
const original = jest.requireActual("../../config"); | ||
return { | ||
...original, | ||
webpack_loader: "esbuild", | ||
additional_paths: [...original.additional_paths, "node_modules/included"], | ||
}; | ||
}); | ||
|
||
const createWebpackConfig = (file, use) => { | ||
return { | ||
entry: { file }, | ||
module: { | ||
rules: [ | ||
{ | ||
...esbuildConfig, | ||
use, | ||
}, | ||
], | ||
}, | ||
output: { | ||
path: "/", | ||
filename: "scripts-bundled.js", | ||
}, | ||
}; | ||
}; | ||
|
||
describe("swc", () => { | ||
test("process source path", async () => { | ||
const normalPath = `${app_javascript}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler( | ||
createWebpackConfig(normalPath, loader) | ||
); | ||
await compiler.run(); | ||
expect(tracked[normalPath]).toBeTruthy(); | ||
}); | ||
|
||
test("exclude node_modules", async () => { | ||
const ignored = `${node_modules}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(ignored, loader)); | ||
await compiler.run(); | ||
expect(tracked[ignored]).toBeUndefined(); | ||
}); | ||
|
||
test("explicitly included node_modules should be transpiled", async () => { | ||
const included = `${node_modules_included}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(included, loader)); | ||
await compiler.run(); | ||
expect(tracked[included]).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const path = require("path"); | ||
const { | ||
app_javascript, | ||
node_modules, | ||
node_modules_included, | ||
createTestCompiler, | ||
createTrackLoader, | ||
} = require("./__utils__/webpack"); | ||
const swcConfig = require("../swc"); | ||
|
||
jest.mock("../../config", () => { | ||
const original = jest.requireActual("../../config"); | ||
return { | ||
...original, | ||
webpack_loader: "swc", | ||
additional_paths: [...original.additional_paths, "node_modules/included"], | ||
}; | ||
}); | ||
|
||
const createWebpackConfig = (file, use) => { | ||
return { | ||
entry: { file }, | ||
module: { | ||
rules: [ | ||
{ | ||
...swcConfig, | ||
use, | ||
}, | ||
], | ||
}, | ||
output: { | ||
path: "/", | ||
filename: "scripts-bundled.js", | ||
}, | ||
}; | ||
}; | ||
|
||
describe("swc", () => { | ||
test("process source path", async () => { | ||
const normalPath = `${app_javascript}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler( | ||
createWebpackConfig(normalPath, loader) | ||
); | ||
await compiler.run(); | ||
expect(tracked[normalPath]).toBeTruthy(); | ||
}); | ||
|
||
test("exclude node_modules", async () => { | ||
const ignored = `${node_modules}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(ignored, loader)); | ||
await compiler.run(); | ||
expect(tracked[ignored]).toBeUndefined(); | ||
}); | ||
|
||
test("explicitly included node_modules should be transpiled", async () => { | ||
const included = `${node_modules_included}/a.js`; | ||
const [tracked, loader] = createTrackLoader(); | ||
const compiler = createTestCompiler(createWebpackConfig(included, loader)); | ||
await compiler.run(); | ||
expect(tracked[included]).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,12 @@ | ||
const { resolve } = require('path') | ||
const { realpathSync } = require('fs') | ||
const { loaderMatches } = require('../utils/helpers') | ||
const { getEsbuildLoaderConfig } = require('../esbuild') | ||
|
||
const { | ||
source_path: sourcePath, | ||
additional_paths: additionalPaths, | ||
webpack_loader: webpackLoader | ||
} = require('../config') | ||
const jscommon = require('./jscommon') | ||
|
||
module.exports = loaderMatches(webpackLoader, 'esbuild', () => ({ | ||
test: /\.(ts|tsx|js|jsx|mjs|coffee)?(\.erb)?$/, | ||
include: [sourcePath, ...additionalPaths].map((p) => { | ||
try { | ||
return realpathSync(p) | ||
} catch (e) { | ||
return resolve(p) | ||
} | ||
}), | ||
exclude: /node_modules/, | ||
...jscommon, | ||
use: ({ resource }) => getEsbuildLoaderConfig(resource) | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { resolve } = require('path') | ||
const { realpathSync } = require('fs') | ||
const { | ||
source_path: sourcePath, | ||
additional_paths: additionalPaths | ||
} = require('../config') | ||
|
||
const inclusions = [sourcePath, ...additionalPaths].map(p => { | ||
try { | ||
return realpathSync(p) | ||
} catch (e) { | ||
return resolve(p) | ||
} | ||
}) | ||
|
||
module.exports = { | ||
include: inclusions, | ||
exclude: [ | ||
{ | ||
// exclude all node_modules from running through babel-loader | ||
and: [resolve('node_modules')], | ||
// Do not exclude inclusions, as otherwise these won't be transpiled | ||
not: [...inclusions] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,12 @@ | ||
const { resolve } = require('path') | ||
const { realpathSync } = require('fs') | ||
const { loaderMatches } = require('../utils/helpers') | ||
const { getSwcLoaderConfig } = require('../swc') | ||
|
||
const { | ||
source_path: sourcePath, | ||
additional_paths: additionalPaths, | ||
webpack_loader: webpackLoader | ||
} = require('../config') | ||
const jscommon = require('./jscommon') | ||
|
||
module.exports = loaderMatches(webpackLoader, 'swc', () => ({ | ||
test: /\.(ts|tsx|js|jsx|mjs|coffee)?(\.erb)?$/, | ||
include: [sourcePath, ...additionalPaths].map((p) => { | ||
try { | ||
return realpathSync(p) | ||
} catch (e) { | ||
return resolve(p) | ||
} | ||
}), | ||
exclude: /node_modules/, | ||
...jscommon, | ||
use: ({ resource }) => getSwcLoaderConfig(resource) | ||
})) |
Oops, something went wrong.