-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from davidchin/move_src
CHECKOUT-2739: Move `ScriptLoader` source files into separate repository
- Loading branch information
Showing
15 changed files
with
4,993 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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,10 @@ | ||
## What? | ||
... | ||
|
||
## Why? | ||
... | ||
|
||
## Testing / Proof | ||
... | ||
|
||
@bigcommerce/frontend |
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,3 @@ | ||
*.log | ||
/coverage | ||
/node_modules |
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,13 @@ | ||
language: node_js | ||
|
||
node_js: 6 | ||
|
||
cache: yarn | ||
|
||
dist: trusty | ||
|
||
sudo: false | ||
|
||
script: | ||
- yarn lint | ||
- yarn test:series -- --coverage |
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,6 @@ | ||
{ | ||
"scopes": [ | ||
"common", | ||
"core" | ||
] | ||
} |
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,23 @@ | ||
module.exports = { | ||
browser: true, | ||
transform: { | ||
'\\.(ts|js)$': '<rootDir>/node_modules/ts-jest/preprocessor.js', | ||
}, | ||
moduleFileExtensions: [ | ||
'ts', | ||
'tsx', | ||
'js', | ||
'jsx', | ||
'json', | ||
], | ||
testRegex: 'src/.*\\.spec.(js|ts)$', | ||
setupTestFrameworkScriptFile: '<rootDir>/jest-setup.js', | ||
collectCoverageFrom: [ | ||
'src/**/*.{js,ts}', | ||
], | ||
coveragePathIgnorePatterns: [ | ||
'\\.mock\\.(js|ts)$', | ||
'\\.typedef\\.(js|ts)$', | ||
'\\.d\\.ts$', | ||
], | ||
}; |
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,3 @@ | ||
beforeAll(() => { | ||
expect.hasAssertions(); | ||
}); |
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 @@ | ||
{ | ||
"name": "@bigcommerce/script-loader", | ||
"version": "0.0.0", | ||
"description": "A library for loading JavaScript files asynchronously", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib/" | ||
], | ||
"engines": { | ||
"node": "^6.10.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bigcommerce/script-loader-js.git" | ||
}, | ||
"author": "BigCommerce", | ||
"bugs": { | ||
"url": "https://github.com/bigcommerce/script-loader-js/issues" | ||
}, | ||
"homepage": "https://github.com/bigcommerce/script-loader-js", | ||
"scripts": { | ||
"prebuild": "yarn lint && yarn test", | ||
"build": "yarn compile", | ||
"precompile": "rm -rf lib", | ||
"compile": "tsc --outDir lib", | ||
"lint": "tslint src/**/*.ts --config tslint.json && tsc --noEmit", | ||
"prerelease": "git fetch --tags && yarn validate-dependencies && yarn validate-commits && yarn build", | ||
"release": "git add lib && standard-version -a", | ||
"test": "jest --config jest-config.js", | ||
"test:coverage": "yarn test -- --coverage", | ||
"test:series": "yarn test -- --runInBand", | ||
"test:watch": "yarn test -- --watch", | ||
"travis": "yarn lint && yarn test:series -- --coverage", | ||
"validate-commits": "validate-commits", | ||
"validate-dependencies": "yarn install --check-files --frozen-lockfile" | ||
}, | ||
"dependencies": { | ||
"tslib": "^1.8.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^21.1.10", | ||
"jest": "^21.2.1", | ||
"standard-version": "^4.2.0", | ||
"ts-jest": "^21.2.3", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.7.1", | ||
"validate-commits": "git+ssh://[email protected]/bigcommerce-labs/validate-commits.git#1.1.0" | ||
} | ||
} |
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,5 @@ | ||
import ScriptLoader from './script-loader'; | ||
|
||
export default function createScriptLoader(): ScriptLoader { | ||
return new ScriptLoader(document); | ||
} |
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,2 @@ | ||
export { default as ScriptLoader } from './script-loader'; | ||
export { default as createScriptLoader } from './create-script-loader'; |
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,46 @@ | ||
import ScriptLoader from './script-loader'; | ||
|
||
describe('ScriptLoader', () => { | ||
let loader: ScriptLoader; | ||
let script: HTMLScriptElement; | ||
|
||
beforeEach(() => { | ||
script = document.createElement('script'); | ||
|
||
jest.spyOn(document, 'createElement').mockImplementation(() => script); | ||
jest.spyOn(document.body, 'appendChild').mockImplementation((element) => | ||
element.onreadystatechange(new Event('readystatechange')) | ||
); | ||
|
||
loader = new ScriptLoader(document); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('attaches script tag to document', async () => { | ||
await loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js'); | ||
|
||
expect(document.body.appendChild).toHaveBeenCalledWith(script); | ||
expect(script.src).toEqual('https://code.jquery.com/jquery-3.2.1.min.js'); | ||
}); | ||
|
||
it('resolves promise if script is loaded', async () => { | ||
const output = await loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js'); | ||
|
||
expect(output).toBeInstanceOf(Event); | ||
}); | ||
|
||
it('rejects promise if script is not loaded', async () => { | ||
jest.spyOn(document.body, 'appendChild').mockImplementation( | ||
(element) => element.onerror(new Event('error')) | ||
); | ||
|
||
try { | ||
await loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js'); | ||
} catch (output) { | ||
expect(output).toBeInstanceOf(Event); | ||
} | ||
}); | ||
}); |
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,24 @@ | ||
export default class ScriptLoader { | ||
constructor( | ||
private _document: Document | ||
) {} | ||
|
||
loadScript(src: string): Promise<Event> { | ||
return new Promise((resolve, reject) => { | ||
const script = this._document.createElement('script') as LegacyHTMLScriptElement; | ||
|
||
script.onload = (event) => resolve(event); | ||
script.onreadystatechange = (event) => resolve(event); | ||
script.onerror = (event) => reject(event); | ||
script.async = true; | ||
script.src = src; | ||
|
||
this._document.body.appendChild(script); | ||
}); | ||
} | ||
} | ||
|
||
interface LegacyHTMLScriptElement extends HTMLScriptElement { | ||
// `onreadystatechange` is needed to support legacy IE | ||
onreadystatechange: (this: HTMLElement, event: Event) => any; | ||
} |
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,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"experimentalDecorators": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"importHelpers": true, | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"es6", | ||
"scripthost", | ||
], | ||
"moduleResolution": "node", | ||
"removeComments": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"stripInternal": true, | ||
"target": "es5" | ||
}, | ||
"include": [ | ||
"src/**/*.js", | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"src/**/*.*.js", | ||
"src/**/*.*.ts" | ||
] | ||
} |
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,42 @@ | ||
{ | ||
"extends": "tslint:recommended", | ||
"rules": { | ||
"arrow-parens": false, | ||
"grouped-imports": false, | ||
"interface-name": [ | ||
true, | ||
"never-prefix" | ||
], | ||
"max-line-length": false, | ||
"member-access": [ | ||
true, | ||
"no-public" | ||
], | ||
"no-empty": false, | ||
"no-reference": false, | ||
"no-shadowed-variable": false, | ||
"object-literal-sort-keys": false, | ||
"ordered-imports": false, | ||
"quotemark": [ | ||
true, | ||
"single" | ||
], | ||
"trailing-comma": [ | ||
true, | ||
{ | ||
"multiline": { | ||
"arrays": "always", | ||
"functions": "never", | ||
"objects": "always", | ||
"typeLiterals": "always" | ||
} | ||
} | ||
], | ||
"variable-name": [ | ||
true, | ||
"ban-keywords", | ||
"check-format", | ||
"allow-leading-underscore" | ||
] | ||
} | ||
} |
Oops, something went wrong.